package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.makerspace.domain.UserDemandDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.UserDemandMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
@Component
public class UserDemandDAO  extends  BaseDAO<UserDemandDO>{

    @Resource
    private UserDemandMapper userDemandMapper;

    @PostConstruct
    public void init(){
        this.mapper = userDemandMapper;
    }
    public final static String DEMAND_NAME = "demand_name";
    public final static String DEMAND_DESC = "demand_desc";
    public final static String KEYWORD = "keyword";
    public final static String CATEGORY = "category";

        public List<UserDemandDO> selectUserName(String username) {
            return  mapper.selectList(new QueryWrapper<UserDemandDO>().eq("username",username));
    }

    public IPage<UserDemandDO> selectByPageAndLike(Pagination pagination, String demandName) {
        QueryWrapper<UserDemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CATEGORY,CATEGORY).like(DEMAND_NAME,demandName).eq("is_show",1);
        return  mapper.selectPage(new Page<UserDemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<UserDemandDO> selectByPageAndCatgory(Pagination pagination, String category) {
        QueryWrapper<UserDemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category).eq("is_show",1);
        return  mapper.selectPage(new Page<UserDemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<UserDemandDO> selectByPageAndLike2(Pagination pagination, String demandName, String category) {
        QueryWrapper<UserDemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",1).eq("category",category).like("demand_name",demandName);
        return  mapper.selectPage(new Page<UserDemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public  IPage<UserDemandDO> selectByPage(Pagination pagination,String username) {
        QueryWrapper<UserDemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return  mapper.selectPage(new Page<UserDemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }

    public IPage<UserDemandDO> selectByPageAndShow(Pagination pagination, String username, String isShow) {
        QueryWrapper<UserDemandDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_show",isShow).eq("username",username);
        return  mapper.selectPage(new Page<UserDemandDO>(pagination.getPageNum(), pagination.getSize()),queryWrapper);
    }
}
